﻿// MFCApplication2Doc.cpp: CMFCApplication2Doc 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#include "MFCApplication2.h"

#include "MFCApplication2Doc.h"
#include "PlaneCommand.h"
#include "CylinderCommand.h"
#include <propkey.h>

// CMFCApplication2Doc

IMPLEMENT_DYNCREATE(CMFCApplication2Doc, CDocument)

BEGIN_MESSAGE_MAP(CMFCApplication2Doc, CDocument)
	ON_COMMAND(ID_DEMO_PLANE, &CMFCApplication2Doc::OnDemoPlane)
	ON_COMMAND(ID_EDIT_UNDO, &CMFCApplication2Doc::OnEditUndo)
	ON_COMMAND(ID_DEMO_CYLINDER, &CMFCApplication2Doc::OnDemoCylinder)
	ON_COMMAND(ID_EDIT_REDO, &CMFCApplication2Doc::OnEditRedo)
	ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, &CMFCApplication2Doc::OnUpdateEditUndo)
	ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, &CMFCApplication2Doc::OnUpdateEditRedo)
END_MESSAGE_MAP()

// CMFCApplication2Doc 构造/析构

CMFCApplication2Doc::CMFCApplication2Doc() noexcept
{
	Handle(Graphic3d_GraphicDriver) theGraphicDriver = ((CMFCApplication2App*)AfxGetApp())->GetGraphicDriver();

	myViewer = new V3d_Viewer(theGraphicDriver);
	myViewer->SetDefaultLights();
	myViewer->SetLightOn();

	myAISContext = new AIS_InteractiveContext(myViewer);

	myAISContext->SetDisplayMode(AIS_Shaded, true);
	myAISContext->SetAutomaticHilight(Standard_False);
}

CMFCApplication2Doc::~CMFCApplication2Doc()
{
}

BOOL CMFCApplication2Doc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: 在此添加重新初始化代码
	// (SDI 文档将重用该文档)

	return TRUE;
}

// CMFCApplication2Doc 序列化

void CMFCApplication2Doc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: 在此添加存储代码
	}
	else
	{
		// TODO: 在此添加加载代码
	}
}

#ifdef SHARED_HANDLERS

// 缩略图的支持
void CMFCApplication2Doc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
	// 修改此代码以绘制文档数据
	dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));

	CString strText = _T("TODO: implement thumbnail drawing here");
	LOGFONT lf;

	CFont* pDefaultGUIFont = CFont::FromHandle((HFONT)GetStockObject(DEFAULT_GUI_FONT));
	pDefaultGUIFont->GetLogFont(&lf);
	lf.lfHeight = 36;

	CFont fontDraw;
	fontDraw.CreateFontIndirect(&lf);

	CFont* pOldFont = dc.SelectObject(&fontDraw);
	dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
	dc.SelectObject(pOldFont);
}

// 搜索处理程序的支持
void CMFCApplication2Doc::InitializeSearchContent()
{
	CString strSearchContent;
	// 从文档数据设置搜索内容。
	// 内容部分应由“;”分隔

	// 例如:     strSearchContent = _T("point;rectangle;circle;ole object;")；
	SetSearchContent(strSearchContent);
}

void CMFCApplication2Doc::SetSearchContent(const CString& value)
{
	if (value.IsEmpty())
	{
		RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
	}
	else
	{
		CMFCFilterChunkValueImpl* pChunk = nullptr;
		ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
		if (pChunk != nullptr)
		{
			pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
			SetChunkValue(pChunk);
		}
	}
}

#endif // SHARED_HANDLERS

// CMFCApplication2Doc 诊断

namespace {
	int IntervalRandom(int start, int end)
	{
		srand((unsigned)time(NULL));

		return start + rand() % (end - start);
	}

	gp_Pnt RandomPosition()
	{
		return gp_Pnt(IntervalRandom(-400, 400), IntervalRandom(-400, 400), IntervalRandom(-400, 400));
	}

	gp_Dir RandomDirection()
	{
		gp_Dir dirTable[] = { gp::DX(), gp::DY(), gp::DZ() };

		srand((unsigned)time(NULL));

		return dirTable[rand() % 3];
	}

	Quantity_Color RandomColor()
	{
		Quantity_Color clrTable[] = {
			Quantity_NOC_GREEN,
			Quantity_NOC_HOTPINK,
			Quantity_NOC_IVORY,
			Quantity_NOC_CYAN,
			Quantity_NOC_GRAY,
			Quantity_NOC_LIGHTYELLOW,
			Quantity_NOC_RED,
			Quantity_NOC_YELLOW,
			Quantity_NOC_ORCHID
		};

		srand((unsigned)time(NULL));

		int clrNum = sizeof(clrTable) / sizeof(clrTable[0]);
		return clrTable[rand() % clrNum];
	}
}

#ifdef _DEBUG
void CMFCApplication2Doc::AssertValid() const
{
	CDocument::AssertValid();
}

void CMFCApplication2Doc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

// CMFCApplication2Doc 命令

void CMFCApplication2Doc::OnDemoPlane()
{
	gp_Pnt position = RandomPosition();
	gp_Dir normal = RandomDirection();
	Standard_Real size = 200;
	Quantity_Color color = RandomColor();

	auto pCommand = std::make_shared<PlaneCommand>(myAISContext, position, normal, size, color);
	m_undoMgr.DoCommand(pCommand);

	myViewer->Redraw();
}

void CMFCApplication2Doc::OnDemoCylinder()
{
	gp_Pnt position = RandomPosition();
	gp_Dir direction = RandomDirection();
	Standard_Real radius = 50;
	Standard_Real height = 200;
	Quantity_Color color = RandomColor();

	auto pCommand = std::make_shared<CylinderCommand>(myAISContext, position, direction, radius, height, color);
	m_undoMgr.DoCommand(pCommand);

	myViewer->Redraw();
}

void CMFCApplication2Doc::OnEditUndo()
{
	m_undoMgr.Undo();
	myViewer->Redraw();
}

void CMFCApplication2Doc::OnEditRedo()
{
	m_undoMgr.Redo();
	myViewer->Redraw();
}

void CMFCApplication2Doc::OnUpdateEditUndo(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(m_undoMgr.CanUndo());
}

void CMFCApplication2Doc::OnUpdateEditRedo(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(m_undoMgr.CanRedo());
}